package com.link.base.base.authproduct.service;

import com.link.base.base.authproduct.dao.mybatis.mapper.AuthProductMapper;
import com.link.base.base.authproduct.model.AuthProduct;
import com.link.base.base.authproduct.model.AuthProductQuery;
import com.link.base.base.product.model.PriceListItem;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author liushoulong
 * @date 2018-11-06
 * @desc 授权单品服务
 */
@Service
public class AuthProductServiceImpl extends BasicServiceImpl<AuthProduct> implements AuthProductService {

    /**未生效**/
    private static final  String AUTH_PRODUCT_STATUS_NEW = "New";

    @Resource
    private AuthProductMapper authProductMapper;

    @Override
    public BasicMapper<AuthProduct> getBasicMapper() {
        return authProductMapper;
    }

    /**
     * 传入产品列表，批量生成授权单品记录
     *
     * @param authProductQuery
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void batchInsertByProdList(AuthProductQuery authProductQuery) throws Exception {
        if (Objects.isNull(authProductQuery)) {
            throw new ServiceException("PUBLIC-015");
        }
        Long headId = authProductQuery.getHeadId();
        if (null == headId) {
            throw new ServiceException("AUTH-001");
        }
        List<PriceListItem> prodList = authProductQuery.getProdList();
        if (CollectionUtils.isEmpty(prodList)) {
            throw new ServiceException("AUTH-002");
        }
        for (PriceListItem p : prodList) {
            AuthProduct autoProd = new AuthProduct();
            autoProd.setProdId(p.getProdId());
            autoProd.setAutoBrandId(headId);
            autoProd.setProductStatus(AUTH_PRODUCT_STATUS_NEW);
            insert(autoProd);
        }
    }
}
